package com.yaoxun.bored.controller.sys;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.Constant;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.menu.Menu;
import com.yaoxun.bored.model.sys.menu.MenuQuery;
import com.yaoxun.bored.model.sys.menu.MenuTree;
import com.yaoxun.bored.service.sys.menu.MenuService;
import com.yaoxun.bored.util.enums.JsonStatus;

@RequestMapping("/menu")
@Controller
public class MenuController extends BaseController {

	private static final String INDEX_PAGE = "sys/menu/menu";
	
	private static final String EDIT_PAGE = "sys/menu/menu_edit";
	
	private static final String TREE_PAGE = "sys/menu/menu_tree";
	
	private static final String CONF_auth = "sys/auth/auth";
	
	@Inject
	private MenuService menuService;
	
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public ModelAndView save() {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		return mv;
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(Menu menu) {
		JsonObject jsonObject = new JsonObject();
		if(false == commValidate(menu, jsonObject)) {
			return jsonObject;
		}
		try {			
			preSaveOrEdit(menu);
			menu.setCreateId(getLoginUserId());
			menu.setCreateTime(new Date());
			menuService.insertMenu(menu);
			return jsonObject;
		} catch (IllegalArgumentException e) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, e.getMessage());
			return jsonObject;
		}
	}
	
	private void preSaveOrEdit(Menu menu) {
		if(menu.getParentId().intValue() != 0) {
			Menu parent = menuService.selectByPrimaryKey(menu.getParentId());
			if(parent != null) {
				menu.setParentName(parent.getMenuName());
				menu.setPath(parent.getPath());
				menu.setLevel(parent.getLevel().intValue() + 1); //增加一级
			}else {
				throw new IllegalArgumentException("上级菜单参数不合法");
			}
		}
	}
		 
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		Menu menu = menuService.selectByPrimaryKey(id);
		mv.addObject(Constant.MV_OBJ_NAME, menu);
		return mv;
	}
	
	/**
	 * 编辑菜单
	 * @author Loren
	 * @createTime 2018年2月6日 上午11:35:33
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject edit(Menu menu) {
		JsonObject jsonObject = new JsonObject();
		try {			
			preSaveOrEdit(menu);
			menu.setModifyId(getLoginUserId());
			menu.setModifyTime(new Date());
			menuService.updateById(menu);
		} catch(IllegalArgumentException e) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, e.getMessage());
			return jsonObject;
		}
		return jsonObject;
	}
	
	
	@RequestMapping(value="/search")
	@ResponseBody
	public PageResult search(MenuQuery query) {
		query.setOrderBy("sort asc");
		PageInfo<Map<String, Object>> info = menuService.selectByPaging(query);
		PageResult pageResult = new PageResult(info.getTotal(), info.getList());		
		return pageResult;
	}
	
	/**
	 * 删除
	 * @author Loren
	 * @createTime 2018年2月8日 上午10:00:50
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject delete(@RequestParam Integer menuId) {
		JsonObject jsonObject = new JsonObject();
		Menu menu = menuService.selectByPrimaryKey(menuId);
		if(menu != null) {			
			menuService.deleteMenu(menu);
		}
		return jsonObject;
	}
	
	/**
	 * 配置权限
	 * @author Loren
	 * @createTime 2018年2月8日 下午12:05:30
	 * @return
	 */
	@RequestMapping(value="/conf_auth", method=RequestMethod.GET)
	public ModelAndView confAuth(@RequestParam Integer menuId) {
		ModelAndView mv = new ModelAndView(CONF_auth);
		mv.addObject("menuId", menuId);
		return mv;
	}
	
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(INDEX_PAGE);
		return mv;
	}
	
	
	/**
	 * 跳转到菜单树
	 * @author Loren
	 * @createTime 2018年2月5日 下午4:06:36
	 * @return
	 */
	@RequestMapping(value="/menutree", method=RequestMethod.GET)
	public ModelAndView menuTree() {
		ModelAndView mv = new ModelAndView(TREE_PAGE);
		return mv;
	}
	
	/**
	 * 查询出父节点的下一级节点
	 * @author Loren
	 * @createTime 2018年2月5日 下午4:10:12
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/children", method=RequestMethod.GET)
	@ResponseBody
	public  List<MenuTree> menuTree(@RequestParam(defaultValue = "0") Integer parentId) {
		return menuService.selectByPid(parentId);
	}
}
